package com.erik.ipmonitor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.erik.ipmonitor.listener.ProjectExcelListener;
import com.erik.ipmonitor.model.excel.ProjectExcel;
import com.erik.ipmonitor.model.po.Project;
import com.erik.ipmonitor.model.vo.SearchIndexVO;
import com.erik.ipmonitor.repository.ProjectRepository;
import com.erik.ipmonitor.service.ISearchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * SearchServiceImpl
 *
 * @author : erik.zhou
 * @date : 2025/08/25/16:16
 * @description :
 */
@Service
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private ProjectRepository projectRepository;

    @Override
    public Page<SearchIndexVO> doSearch(int page, int size, String keyword) {
        size = size < 1 ? 50 : size;
        // 先根据名称查询
        String query = parseKeyword(keyword);
        Page<Project> projectPage = projectRepository.findByKeyword(query, PageRequest.of(page, size));
        // 转换为VO
        Page<SearchIndexVO> searchIndexVOPage = projectPage.map(project -> BeanUtil.copyProperties(project, SearchIndexVO.class));
        return searchIndexVOPage;
    }


    @Override
    public void loadFile(MultipartFile file, String encoding) {
        // 先清空数据库中的数据
        projectRepository.deleteAll();
        try {
            EasyExcel.read(file.getInputStream(), ProjectExcel.class, new ProjectExcelListener(projectRepository))
                    .excelType(ExcelTypeEnum.CSV)
                    .charset(Charset.forName(encoding))
                    .sheet()
                    .doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 解析用户的输入，支持 & 或 | 分隔
     * @param keyword
     * @return
     */
    private String parseKeyword(String keyword) {
        StringBuilder result = new StringBuilder();
        if (StringUtils.isBlank(keyword)) {
            return "1=1";
        }
        if (StringUtils.contains(keyword, "&")) {
            String[] split = keyword.split("&");
            for (String s : split) {
                if (StringUtils.isBlank(s)) {
                    continue;
                }
                s = s.trim();
                result.append(" project_team like ").append("\"%").append(s).append("%\"").append(" AND ");
            }
            // 去掉最后一个 AND
            result.delete(result.length() - 4, result.length());
        } else if (StringUtils.contains(keyword, "|")) {
            String[] split = keyword.split("\\|");
            for (String s : split) {
                if (StringUtils.isBlank(s)) {
                    continue;
                }
                s = s.trim();
                result.append(" project_team like ").append("\"%").append(s).append("%\"").append(" OR ");
            }
            // 去掉最后一个 OR
            result.delete(result.length() - 3, result.length());
        } else if (StringUtils.contains(keyword, " ")) {
            String[] split = keyword.split(" ");
            for (String s : split) {
                if (StringUtils.isBlank(s)) {
                    continue;
                }
                s = s.trim();
                result.append(" project_team like ").append("\"%").append(s).append("%\"").append(" OR ");
            }
            // 去掉最后一个 OR
            result.delete(result.length() - 4, result.length());
        }else {
            result.append(" project_team like ").append("\"%").append(keyword).append("%\"");
        }
        return result.toString();
    }
}
